package com.chee.plant.hibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.chee.plant.hibernate.object.FarmPlant;
import com.chee.plant.server.HibernateSessionFactory;

public class FarmPlantDAO extends BaseHibernateDAO {

	/**
	 * 获取全部农场植物
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<FarmPlant> getAll() {
		Session session = HibernateSessionFactory.getSession();
		String hql = "select farmPlant from FarmPlant as farmPlant";
		Query query = session.createQuery(hql);
		List<FarmPlant> list = query.list();
		return list;
	}

	/**
	 * 根据用户Id、x y 坐标查询农场植物 （考虑如下：因为每块地里只可能同时有一个植物，再加上地的主人，就能确定是什么样的植物。）
	 * 
	 * @param userId
	 * @param coordX
	 * @param coordY
	 * @return 特定的农场植物
	 */
	public FarmPlant getFarmPlantByUserIdAndCroodXY(Integer userId,
			Integer coordX, Integer coordY) {
		Session session = HibernateSessionFactory.getSession();
		String hql = "select farmPlant from FarmPlant as farmPlant where farmPlant.UserId=:userId and farmPlant.CoordX=:coordx and farmPlant.CoordY=:coordy";
		Query query = session.createQuery(hql);
		query.setInteger("userId", userId);
		query.setInteger("coordx", coordX);
		query.setInteger("coordy", coordY);
		FarmPlant farmPlant = (FarmPlant) query.uniqueResult();
		return farmPlant;
	}

	/**
	 * 主方法，用于测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// 1 测试getAll()
		List<FarmPlant> list = new FarmPlantDAO().getAll();
		System.out.println(list.get(0).getGrowTime());
		
		//2 测试 getFarmPlantByUserIdAndCroodXY(Integer userId,Integer coordX, Integer coordY)
		FarmPlant farmPlant=new FarmPlantDAO().getFarmPlantByUserIdAndCroodXY(1, 1, 3);
		
		System.out.println(farmPlant.getCoordX());
		System.out.println(farmPlant.getCoordY());
		System.out.println(farmPlant.getGrowTime());
		System.out.println(farmPlant.getId());
		System.out.println(farmPlant.getUserId());
		System.out.println(farmPlant.getType());
		System.out.println(farmPlant.getUserplantId());
	}

}
